package com.example.caoh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.example.caoh.service.IService;

import tk.mybatis.mapper.common.Mapper;

public class BaseService<T> implements IService<T> {

	@Autowired
    private Mapper<T> mapper;//泛型装配

	/**
     * 主键查询
     */
	@Override
    public T getOneById(Object id) {
        return  mapper.selectByPrimaryKey(id);
    }
    
    /**
     * 单条查询
     */
	@Override
    public T getOne(T entity) {
        return  mapper.selectOne(entity);
    }

    /**
     * 主键更新
     */
	@Override
	@Transactional
    public int updateOne(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 单条保存
     */
	@Override
	@Transactional
    public int saveOne(T entity) {
        return mapper.insertSelective(entity);
    }

    /**
     * 主键删除
     */
    @Override
    @Transactional
    public int deleteOne(Object id) {
        return mapper.deleteByPrimaryKey(id);
    }
    
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }
    
    public List<T> queryAll() {
        return mapper.selectAll();
    }
}
